import java.util.ArrayList;
import java.util.List;

import akka.actor.UntypedActor;

/**
 * Holding cell for passengers failing baggage or body scan
 * @author mja2637
 */
public class Jail extends UntypedActor {
	/** Stock indentation for messages from this actor */
	private final String INDENTATION = "        ";
	/** The number of lines/security stations feeding this jail */
	private final int numFeederLines;
	/** List of inmates contained in this jail */
	private final List<Passenger> inmates;
	/** Tracks poison pills received */
	private int pillCount = 0;

	/**
	 * Constructs jail, setting final fields
	 * @param numLines feeding this jail
	 */
	public Jail(int numLines) { 
		this.inmates = new ArrayList<Passenger>();
		this.numFeederLines = numLines; 
	}

	@Override
	public void onReceive(Object message) throws Exception {
		if (message instanceof Passenger) {
			System.out.format("%sJail: %s jailed\n", INDENTATION, ((Passenger)message).getName());
			inmates.add((Passenger)message);
		} else if (message instanceof Pill) { 
			System.out.format("%sJail: Close received (%d of %d lines)\n", 
					INDENTATION, pillCount+1, numFeederLines);
			if (++pillCount >= numFeederLines) { // Exit when both pills received
				System.out.format("%sJail: Incarcerated Passengers\n", INDENTATION);
				for (Passenger passenger : inmates) System.out.format("%s       %s\n", INDENTATION,passenger.getName());
				System.out.format("%sJail: Closed\n", INDENTATION);
				getContext().stop();
			}
		}
	}
}
